<template>
  <div style="text-align: center;" v-title data-title="编辑文章">
    <h1 class="gray">
      编 辑 文 章
    </h1>
    <el-divider></el-divider>
    <div class="m-content">

      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="标题" prop="title">
          <el-input v-model="ruleForm.title"></el-input>
        </el-form-item>

        <el-form-item label="摘要" prop="description">
          <el-input type="textarea" v-model="ruleForm.description"></el-input>
        </el-form-item>

        <el-form-item label="内容" prop="content">
          <mavon-editor v-model="ruleForm.content"></mavon-editor>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="back">返 回</el-button>
          <el-button type="primary" @click="submitForm('ruleForm')">保 存</el-button>
          <el-button @click="resetForm('ruleForm')">重 置</el-button>
        </el-form-item>
      </el-form>

    </div>

  </div>
</template>

<script>
export default {
  name: 'newsedit',

  data() {
    return {
      ruleForm: {
        id: '',
        title: '',
        description: '',
        content: ''
      },
      rules: {
        title: [
          {required: true, message: '请输入标题', trigger: 'blur'},
          {min: 3, max: 25, message: '长度在 3 到 25 个字符', trigger: 'blur'}
        ],
        description: [
          {required: true, message: '请输入摘要', trigger: 'blur'}
        ],
        content: [
          {trequired: true, message: '请输入内容', trigger: 'blur'}
        ]
      }
    }
  },

  //渲染时 读取用户数据
  created() {
    const newsId = this.$route.params.newsId
    console.log(newsId)
    const _this = this
    if (newsId) {
      this.$axios.get('/sys/news/' + newsId).then(res => {
        const news = res.data.data
        _this.ruleForm.id = news.id
        _this.ruleForm.title = news.title
        _this.ruleForm.description = news.description
        _this.ruleForm.content = news.content
      })
    }

  },
  methods: {
    //修改文章
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const _this = this
          this.$axios.post('sys/news/edit/save', this.ruleForm).then(res => {
            console.log(res)
            _this.$alert('修改成功', '提示', {
              confirmButtonText: '确定',
              callback: action => {
                _this.$router.push("/sys/news")
              }
            });

          })

        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },

    //返回
    back() {
      this.$router.push("/sys/news")
    },

    //重置
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  },
  getNews(id) {
    this.$axios.get('/sys/news/:newsId/edit').then(res => {

    })
  }

}
</script>

<style>

</style>
